package com.collect.senegal.zhiScene.zhiTelemarketing.zhiLogic;

import com.collect.senegal.MyApplication;
import com.zhi.inisyccalllog.beans.ASCallLogInfoBean;
import com.zhi.inisyccalllog.services.ASCallLogInfo;

import java.util.ArrayList;
import java.util.HashMap;

public class ZHITelemarketingFollowHelper {

    public static final String FOLLOW_STATE_FOLLOWED = "FOLLOWED";
    public static final String FOLLOW_STATE_FOCUS_ON = "FOCUS_ON";

    public static final int FOLLOW_TYPE_NONE = 0;
    public static final int FOLLOW_TYPE_PHONE_CALL = 1;
    public static final int FOLLOW_TYPE_WHATSAPP = 2;

    private static final HashMap<String, Long> mPhoneCallCacheMap = new HashMap<>();
    private static final HashMap<String, Long> mWhatsappCacheMap = new HashMap<>();

    private static String mCurProduct;
    private static String mCurMobile;

    /**
     * 是否在跟进
     */
    public static int FOLLOW_TYPE = FOLLOW_TYPE_NONE;

    private static class InnerZHITelemarketingFollowHelper {
        private static final ZHITelemarketingFollowHelper instance = new ZHITelemarketingFollowHelper();
    }

    public static ZHITelemarketingFollowHelper getInstance() {
        return InnerZHITelemarketingFollowHelper.instance;
    }

    /**
     * 电话跟进开始
     *
     * @param product
     * @param mobile
     */
    public void setFollowPhoneCallStart(String product, String mobile) {
        mCurProduct = product;
        mCurMobile = mobile;
        String targetUser = createFollowStartKey(product, mobile);
        long curMillis = System.currentTimeMillis();
        mPhoneCallCacheMap.put(targetUser, curMillis);

        String targetUser_end = createFollowEndKey(product, mobile);
        mPhoneCallCacheMap.put(targetUser_end, 0L);
    }

    /**
     * whatsapp 跟进开始
     *
     * @param product
     * @param mobile
     */
    public void setFollowWhatsappStart(String product, String mobile) {
        mCurProduct = product;
        mCurMobile = mobile;
        String targetUser = createFollowStartKey(product, mobile);
        long curMillis = System.currentTimeMillis();
        mWhatsappCacheMap.put(targetUser, curMillis);

        String targetUser_end = createFollowEndKey(product, mobile);
        mWhatsappCacheMap.put(targetUser_end, 0L);
    }

    /**
     * 查询是否已跟进 phone call
     *
     * @param product
     * @param mobile
     * @return
     */
    public boolean checkDidFollowedPhoneCall(String product, String mobile) {
        ASCallLogInfoBean callLogInfoBean = checkCallRecord(mobile);
        if (callLogInfoBean != null) {
            //1 通话时长超过10秒
            if (callLogInfoBean.getDurationInt() >= 8) {
                return true;
            }

            //1 拨打电话超过10秒
            String targetUser = createFollowStartKey(product, mobile);
            String targetUser_end = createFollowEndKey(product, mobile);

            Long startPhoneCallMillis = mPhoneCallCacheMap.get(targetUser);
            if (startPhoneCallMillis != null && (startPhoneCallMillis > 0)) {
                Long endPhoneCallMillis = mPhoneCallCacheMap.get(targetUser_end);
                return endPhoneCallMillis != null && ((endPhoneCallMillis - startPhoneCallMillis) > 10000);
            }
        }
        return false;
    }

    /**
     * 删除跟进记录
     *
     * @param product
     * @param mobile
     */
    public void removeFollowCache(String product, String mobile) {
        String targetUser = createFollowStartKey(product, mobile);
        String targetUser_end = createFollowEndKey(product, mobile);

        try {
            mPhoneCallCacheMap.remove(targetUser);
            mPhoneCallCacheMap.remove(targetUser_end);

            mWhatsappCacheMap.remove(targetUser);
            mWhatsappCacheMap.remove(targetUser_end);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清空所有缓存
     */
    public void removeAllCache() {
        try {
            mPhoneCallCacheMap.clear();
            mWhatsappCacheMap.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refershFollowCache() {
        if (FOLLOW_TYPE == FOLLOW_TYPE_NONE) {
            reset();
            return;
        }

        if (FOLLOW_TYPE == FOLLOW_TYPE_PHONE_CALL) {
            String targetUser_end = createFollowEndKey(mCurProduct, mCurMobile);
            long curMillis = System.currentTimeMillis();
            mPhoneCallCacheMap.put(targetUser_end, curMillis);
            reset();
            return;
        }

//        if (FOLLOW_TYPE == FOLLOW_TYPE_WHATSAPP) {
//            String targetUser_end = createFollowEndKey(mCurProduct, mCurMobile);
//            long curMillis = System.currentTimeMillis();
//            mWhatsappCacheMap.put(targetUser_end, curMillis);
//            reset();
//        }
    }

    public String createFollowStartKey(String product, String mobile) {
        return product + "_" + mobile + "_" + "START";
    }

    public String createFollowEndKey(String product, String mobile) {
        return product + "_" + mobile + "_" + "END";
    }

    public void reset() {
        FOLLOW_TYPE = FOLLOW_TYPE_NONE;
        mCurProduct = null;
        mCurMobile = null;
    }

    public ASCallLogInfoBean checkCallRecord(String mobile) {
        try {
            ArrayList<ASCallLogInfoBean> callLogInfoBeans = ASCallLogInfo.getList(MyApplication.getAppContext());
            for (int i = 0; i < callLogInfoBeans.size(); i++) {
                ASCallLogInfoBean callLogInfoBean = callLogInfoBeans.get(i);
                if (callLogInfoBean.getMobile().contains(mobile)) {
                    return callLogInfoBean;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
